package hluck.me.srorage.sys.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import hluck.me.srorage.sys.common.DataGridView;
import hluck.me.srorage.sys.common.ResultObj;
import hluck.me.srorage.sys.domain.SysLogLogin;
import hluck.me.srorage.sys.service.SysLogLoginService;
import hluck.me.srorage.sys.vo.LogInfoVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 老雷
 * @since 2021-02-15
 */
@RestController
@RequestMapping("/sys/sys-log-login")
public class SysLogLoginController {
    @Autowired
    private SysLogLoginService logLoginService;

    /**
     * 全查询
     */
    @RequestMapping("loadAllLog")
    public DataGridView loadAllLogInfo(LogInfoVo logInfoVo){
        IPage<SysLogLogin> page=new Page<>(logInfoVo.getPage(),logInfoVo.getLimit());

        QueryWrapper<SysLogLogin> queryWrapper=new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(logInfoVo.getLoginname()),"loginname",logInfoVo.getLoginname());
        queryWrapper.like(StringUtils.isNotBlank(logInfoVo.getLoginip()),"loginip",logInfoVo.getLoginip());
        queryWrapper.ge(logInfoVo.getStartTime()!=null,"logintime",logInfoVo.getStartTime());
        queryWrapper.le(logInfoVo.getEndTime()!=null,"logintime",logInfoVo.getEndTime());
        queryWrapper.orderByDesc("logintime");

        this.logLoginService.page(page,queryWrapper);

        return new DataGridView(page.getTotal(),page.getRecords());
    }

    /**
     * 删除
     */
    @RequestMapping("deleteLogInfo")
    public ResultObj deleteLogInfo(Integer id){
        try{
            this.logLoginService.removeById(id);
            return ResultObj.DELETE_SUCCESS;
        }catch (Exception e){
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }


    /**
     * 批量删除
     */
    @RequestMapping("batchDeleteLogInfo")
    public ResultObj batchDeleteLogInfo(LogInfoVo logInfoVo){
        try{
            ArrayList<Integer> list = new ArrayList<>();
            for (Integer id : logInfoVo.getIds()) {
                list.add(id);
            }
            this.logLoginService.removeByIds(list);
            return ResultObj.DELETE_SUCCESS;
        }catch (Exception e){
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }
}

